﻿
namespace Tentacle.Core
{
    using System;
    using System.Collections.Generic;
    using System.Security.Cryptography;
    using System.Text;

    public class WebSite:IEqualityComparer<WebSite>
    {
        static MD5  md5 = MD5.Create();

        private readonly string _site;
        private readonly string _hash;

        public WebSite(string site)
        {
            _site = site;
            _hash = Convert.ToBase64String(
                md5.ComputeHash(
                    Encoding.UTF8.GetBytes(site)));
        }

        #region Property

        public string Url {
            get {
                return _site;
            }
        }

        public string Id {
            get {
                return _hash;
            }
        }



        #endregion

        #region Override

        public bool Equals(WebSite x, WebSite y)
        {
            return x._hash == y._hash;
        }

        public int GetHashCode(WebSite obj)
        {
            return obj.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if(obj is WebSite site) {
                return site._hash == this._hash;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return _hash.GetHashCode() ^ _site.GetHashCode();
        }

        public override string ToString()
        {
            return $"<{Id},{Url}>";
        }

        #endregion
    }
}
